package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    //分页查询(根据条件)
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return orderService.findPage(queryPageBean);
    }

    //新增体检订单
    @RequestMapping("/add")
    public Result add(@RequestBody Map map,String[] setmealId) {
        if (setmealId == null || setmealId.length != 1) {
            return new Result(false,"套餐必须且只能选一个,请检查");
        }else {
            map.put("setmealId",setmealId[0]);
        }
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            return orderService.order(map);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    //删除体检订单
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.deleteById(id);
            return new Result(true, "删除预约订单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除预约订单失败");
        }
    }
}
